const conn1 = require('./middleware/mysql')
const sqls = require('./middleware/sql')
const app = require('./middleware/http')

// 注册 解析表单的body-parser
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))

// 配置服务端口
const server = app.listen(3000, () => {
    console.log('http://127.0.0.1:3000')
})

const sqlObj = new sqls()

app.get('/api/get_room_info', (req, res) => {
    // console.log(req.query)
    const {
        start_date,
        end_date,
        remain
    } = req.query;
    const sql = sqlObj.search_room_info(
        start_date,
        end_date,
        parseInt(remain)
    )
    conn1.query(sql, (err, result) => {
        console.log(result)
        if (err) return res.json({ err_code: 0, msg: '查询失败', affectedRows: 0 })
        res.json({
            err_code: 1,
            msg: result,
            affectedRows: 0
        })
    })
})

// getById
app.get('/api/take_order', (req, res) => {
    const {
        room_id,
        start,
        end,
        amount,
        payment,
        create_date,
        customer_id,
    } = req.query;
    const sqlStr = sqlObj.take_an_order(
        room_id,
        start,
        end,
        amount,
        payment,
        create_date,
        customer_id
    )
    conn1.query(sqlStr, (err, results) => {
        console.log(err, results)
        if (err) return res.json({ err_code: 1, msg: '获取数据失败', affectedRows: 0 })
        res.json({
            err_code: 1,
            msg: results,
            affectedRows: 0
        })
    })
})